﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
using System.Runtime.InteropServices;

namespace SnapLib
{
   public class ForeignControl : ForeignWindow
   {
      internal IntPtr m_parentWnd;
      internal uint m_controlId;

      public static ForeignControl GetControl(ForeignWindow parent, uint controlId)
      {
         IntPtr hwnd = Win32.GetDlgItem(parent.Handle, controlId);
         if (hwnd != IntPtr.Zero)
         {
            return new ForeignControl(parent, hwnd, controlId);
         }
         return null;
      }

      public static ForeignControl GetControlInAllDescendents(ForeignWindow parent, uint controlId)
      {
         ForeignControl ctrl=GetControl(parent,controlId);
         if(ctrl==null)
         {
            foreach (ForeignWindow child in parent.Children)
            {
               ctrl = GetControlInAllDescendents(child, controlId);
               if (ctrl != null) break;
            }
         }
         return ctrl;
      }

      public ForeignControl(IntPtr parentHwnd,IntPtr hwnd, uint controlId) :
         base(parentHwnd)
      {
         m_parentWnd = parentHwnd;
         m_hwnd = hwnd;
         m_controlId = controlId;
      }

      public ForeignControl(IntPtr parentHwnd, uint controlId) :
         base(parentHwnd)
      {
         m_parentWnd = parentHwnd;
         m_hwnd = Win32.GetDlgItem(parentHwnd, controlId);
         if (m_hwnd == IntPtr.Zero) throw new ArgumentException("Invalid Control Id");
         m_controlId = controlId;


      }

      public ForeignControl(ForeignWindow window) :
         base(window)
      {
         m_parentWnd = window.Parent;
         m_hwnd = window.m_hwnd;
         m_controlId = window.ControlId;
      }

      ~ForeignControl()
      {
      }

      public bool Enabled
      {
         get
         {
            return (GetWindowLong(WindowLongId.GWL_STYLE) & WindowStyles.WS_DISABLED) == 0;
         }
         set
         {
            Win32.EnableWindow(m_hwnd, value);
            UInt32 wlong = GetWindowLong(WindowLongId.GWL_STYLE);
            if (value)
               SetWindowLong(WindowLongId.GWL_STYLE, wlong & (~WindowStyles.WS_DISABLED));
            else
               SetWindowLong(WindowLongId.GWL_STYLE, wlong | WindowStyles.WS_DISABLED);
         }
      }

      public string Text
      {
         get
         {
            StringBuilder sb = new StringBuilder(256);

            //WriteProcessBytes(m_textBuffer, Encoding.UTF8.GetBytes("Test"), 8);
            Win32.GetWindowText(m_hwnd, sb, 256);
            //Win32.SendMessage(m_hwnd, WindowsMessage.WM_GETTEXT, 256, (uint)m_textBuffer);
            Win32.SendMessage(m_hwnd, WindowsMessage.WM_GETTEXT, 256, sb);

            // The textbox doesn't need readprocessbytes
            return sb.ToString();


            /*MessageBox.Show(sb.ToString());
            byte[] MyBuffer = ReadProcessBytes(m_textBuffer, 256);
            string str = Encoding.UTF8.GetString(MyBuffer);
            MessageBox.Show(MyBuffer[0].ToString()+" "+MyBuffer[1].ToString()+" "+MyBuffer[2].ToString()+" ");
            int pos = str.IndexOf("\0");
            if (pos != -1)
               str = str.Substring(0, pos);
            return str;*/
         }
         set
         {
            SendMessage(WindowsMessage.WM_SETTEXT, 0, new StringBuilder(value));
         }
      }
   }

}
